﻿
@{
    ViewData["Title"] = "CGTJReportIndex";
    Layout = "~/Views/Share/_LayoutBody.cshtml";
}

<script src="~/layuiadmin/layui/layui.js"></script>
<script src="~/layuiadmin/layui/layui.all.js"></script>

<div class="list-wall">
    <div class="list-top">
        <div class="layui-btn-group">
            <div class="layui-form-item" style="margin-bottom:5px">
                <div class="layui-input-inline" style="width:75px">
                    <label style="padding-left:20px;padding-top:5px">查询条件:</label>
                </div>
                <div class="layui-input-inline" style="width:200px;z-index:100">
                    <div class="ui-filter" style="width: 200px;">
                        <div   id="btn_Search" class="ui-filter-text">
                            <strong id="StartTime">2018-09-19</strong>  至 <strong id="EndTime">现在</strong>
                            <i authorize="yes" style="float:right;padding-right:2px" class="layui-icon ">&#xe625;</i>
                        </div>
                        <div id="list-Search" class="ui-filter-list" style="width: 450px;">
                            <form id="form1" class="layui-form">
                                <table class="form">
                                    <tr>
                                        <th class="formTitle">开始日期:</th>
                                        <td class="formValue" colspan="3">
                                            <div style="float: left; width: 45%;">
                                                <input type="text" name="StartDate"  autocomplete="off"class="form-control input-sm layui-input" id="StartDate">
                                            </div>
                                            <div style="float: left; width: 10%; text-align: center;">
                                                至:
                                            </div>
                                            <div style="float: left; width: 45%;">
                                                <input id="EndDate" name="EndDate" type="text" autocomplete="off" class="form-control input-sm" />
                                            </div>
                                        </td>
                                    </tr>
                                    <tr>
                                        <th class="formTitle">单据编号:</th>
                                        <td class="formValue" colspan="3">
                                            <input id="BillID" name="BillID" type="text" autocomplete="off" class="form-control input-sm" />
                                        </td>

                                    </tr>
                                    <tr>
                                        <th class="formTitle">供应商:</th>
                                        <td class="formValue" colspan="3">
                                            <input id="SupName" name="SupName" type="text" autocomplete="off" class="form-control input-sm" />
                                        </td>

                                    </tr>
                                    <tr>
                                        <th class="formTitle">仓库:</th>
                                        <td class="formValue" colspan="3">
                                            <input id="StoreName" name="StoreName" type="text" autocomplete="off" class="form-control input-sm" />
                                        </td>

                                    </tr>
                                    <tr>
                                        <th class="formTitle">经手人</th>
                                        <td class="formValue" colspan="3">
                                            <input id="YSName" name="YSName" type="text" autocomplete="off" class="form-control input-sm" />
                                        </td>
                                    </tr>
                                </table>
                            </form>
                            <div  style="float:right;padding-right:5px">
                                <button type="button"  class="layui-btn layui-btn-primary layui-btn-sm" data-type="Clear" id="Clear"><i class="layui-icon ">&#xe9aa;</i>重置</button>
                                <button type="button"  authorize="yes"  class="layui-btn layui-btn-sm" data-type="Search" id="Search"><i class="layui-icon ">&#xe615;</i>查询</button>
                            </div>
                        </div>
                    </div>

                </div>

            </div>
        </div>
        <div class="layui-btn-group" style="float:right">
            <button type="button" authorize="yes" class="layui-btn layui-btn-xs" data-type="Excel" id="Excel"><i class="layui-icon ">&#xe601;</i> 导出</button>
        </div>

    </div>
    <div class="gridPanel">
        <table id="list1" class="table table-condensed"></table>
        <div id="pager1"></div>
    </div>
</div>
<script>$('.list-top').authorizeButton()</script>
<script>

    var datas = "";

    var bodyheight = 0;
    $(function () {
        layui.use(['table', 'form', 'layedit', 'laydate'], function () {
            var form = layui.form
                , layer = layui.layer
                , layedit = layui.layedit
                , laydate = layui.laydate
                , table = layui.table;
            var strdate = new Date().Format("yyyy-MM-dd");
            laydate.render({
                elem: '#StartDate',//制定元素
                type: 'date',
                show: false,//默认显示
                position: 'fixed',
                value: strdate,
                isInitValue: true, //是否允许填充初始值，默认为 true
            });
            laydate.render({
                elem: '#EndDate',//制定元素
                type: 'date',
                show: false,//默认显示
                position: 'fixed',
                isInitValue: true, //是否允许填充初始值，默认为 true
            });
            $("#StartTime").html(strdate);
            datas = $('#form1').formGetModel();
            form.render();
        });
    });
    $(function () {
        bodyheight = document.body.scrollHeight;
        $("#list1").jqGrid({
            styleUI: 'Bootstrap',
            url: '/CGTJReport/InStoreList',
            datatype: "json",
            jsonReader: {
                repeatitems: false,
            },
            colModel: [
                { label: 'id', name: 'id', index: 'id', key: true, width: 75, hidden: true },
                { label: '单据日期', name: 'BillDate', index: 'BillDate', formatter: "date", formatoptions: { newformat: 'Y-m-d' }, width: 100, align: 'center' },
                {
                    label: '单据类型', name: 'BillType', index: 'BillType', width: 100, align: 'center', formatter: function (cellvalue, options, rowObject) {
                        if (cellvalue == "BR") {
                            return '<span class="layui-badge layui-bg-red">采购退货单</span>';
                        } else if (cellvalue == "IS") {
                            return '<span class="layui-badge layui-bg-green">采购入库单</span>';
                        } else {
                            return '<span class="layui-badge layui-bg-orange">未知</span>';
                        }
                    }
                },
                { label: '单据号', name: 'BillID', index: 'BillID', width: 180, align: 'center' },
                { label: '经手人', name: 'YSName', index: 'YSName', width: 80, align: 'center' },
                { label: '供应商', name: 'SupName', index: 'SupName', width: 180, align: 'center' },
                { label: '仓库', name: 'StoreName', index: 'StoreName', width: 120, align: 'center' },
                { label: '金额', name: 'Sum', index: 'Sum', width: 120, align: 'center' },
                { label: '备注', name: 'BZ', index: 'BZ', width: 100, align: 'center' },
                {
                    label: '审核状态', name: 'SHStatus', index: 'SHStatus', width: 100, align: 'center', formatter: function (cellvalue, options, rowObject) {
                        if (cellvalue == "0") {
                            return '<span class="layui-badge layui-bg-red">未审核</span>';
                        } else if (cellvalue == "1") {
                            return '<span class="layui-badge layui-bg-green">已审核</span>';
                        } else {
                            return '<span class="layui-badge layui-bg-orange">未知</span>';
                        }
                    }
                },
                { label: '制单人', name: 'CreateName', index: 'CreateName', width: 100, align: 'center' },
                { label: '审核人', name: 'SHName', index: 'SHName', width: 100, align: 'center' },
                { label: '关联订单号', name: 'OrderBillID', index: 'OrderBillID', width: 100, align: 'center' },
            ],
            width: "99%",
            autowidth: true,
            height: $(window).height() - 150,
            rowNum: 30,//一页显示多少条
            rowList: [10, 20, 30, 50, 100],//可供用户选择一页显示多少条
            pager: '#pager1',//表格页脚的占位符(一般是div)的id
            sortname: 'id',//初始化的时候排序的字段
            sortorder: "desc",//排序方式,可选desc,asc
            mtype: "post",//向后台请求数据的ajax的类型。可选post,get
            viewrecords: true,
            shrinkToFit: false,
            rownumbers: true,
            subGrid: true,  // (1)开启子表格支持
            subGridRowExpanded: function (subgrid_id, row_id) {  // (2)子表格容器的id和需要展开子表格的行id，将传入此事件函数
                var subgrid_table_id;
                subgrid_table_id = subgrid_id + "_t";   // (3)根据subgrid_id定义对应的子表格的table的id

                var subgrid_pager_id;
                subgrid_pager_id = subgrid_id + "_pgr"  // (4)根据subgrid_id定义对应的子表格的pager的id

                // (5)动态添加子报表的table和pager
                $("#" + subgrid_id).html("<table id='" + subgrid_table_id + "' class='scroll'></table><div id='" + subgrid_pager_id + "' class='scroll'></div>");

                //$("#" + subgrid_id).html("<table id='" + subgrid_table_id + "' class='scroll'></table>");

                // (6)创建jqGrid对象
                $("#" + subgrid_table_id).jqGrid({
                    url: "/CGTJReport/InStore_MXList?&id=" + row_id,  // (7)子表格数据对应的url，注意传入的contact.id参数
                    datatype: "json",
                    styleUI: 'Bootstrap',
                    colModel: [
                        { label: 'id', name: 'id', index: 'id', key: true, width: 75, hidden: true },
                        { label: '商品名称', name: 'GoodName', index: 'GoodName', width: 140, align: 'center' },
                        { label: '单位', name: 'DW', index: 'DW', width: 80, align: 'center' },
                        { label: '规格', name: 'GGType', index: 'GGType', width: 100, align: 'center' },
                        { label: '型号', name: 'ModelType', index: 'ModelType', width: 75, align: 'center' },
                        { label: '生产厂家', name: 'SCCJ', index: 'SCCJ', width: 75, align: 'center' },
                        { label: '生产批号', name: 'SCPH', index: 'SCPH', width: 75, align: 'center' },
                        { label: '灭菌批号', name: 'MJPH', index: 'MJPH', width: 75, align: 'center' },
                        { label: '生产日期', name: 'scDate', index: 'scDate', formatter: "date", formatoptions: { newformat: 'Y-m-d' }, width: 75, align: 'center' },
                        { label: '有效期至', name: 'yxqDate', index: 'yxqDate', formatter: "date", formatoptions: { newformat: 'Y-m-d' }, width: 75, align: 'center' },
                        { label: '数量', name: 'Num', index: 'Num', width: 80, align: 'center' },
                        { label: '单价', name: 'Price', index: 'Price', width: 75, align: 'center' },
                        { label: '金额', name: 'Sum', index: 'Sum', width: 80, align: 'center' },
                        { label: '备注', name: 'BZ', index: 'BZ', width: 80, align: 'center' },
                    ],
                    jsonReader: {   // (8)针对子表格的jsonReader设置
                        repeatitems: false
                    },
                    loadComplete: function (xhr) {  //隐藏滚动条
                        $("#" + subgrid_table_id).closest(".ui-jqgrid-bdiv").css({ 'overflow-y': 'hidden' });
                    },
                    width: "100%",
                    autowidth: true,
                    mtype: "post",
                    rowNum: 20,
                    rowList: [10, 20, 30, 50, 100],
                    pager: subgrid_pager_id,
                    sortname: 'id',
                    sortorder: "asc",
                    height: '100%',
                    rownumbers: true,
                    shrinkToFit: false,

                });
            },
            subGridRowColapsed: function (subgrid_id, row_id) {
                // this function is called before removing the data
                var subgrid_table_id;
                subgrid_table_id = subgrid_id + "_t";
                $("#" + subgrid_table_id).remove();
            },
            postData: { 'Search': datas },
            footerrow: true,
            gridComplete: function () {
                //添加一行总计行
                $("tr.footRow2").remove();
                var $footerRow = $("tr.footrow");
                $footerRow.after("<tr role='row' class='footrow footRow2 footrow-ltr ui-widget-content'>" + $footerRow.html() + "</tr>");
                var $newFooterRow = $("tr.footRow2");
                //列数判断
                var rowNum = parseInt($(this).getGridParam('records'));
                if (rowNum > 0) {
                    $(".ui-jqgrid-sdiv").show();
                    //页合计
                    var Sums = jQuery(this).getCol('Sum', false, 'sum');
                    Sums = Math.round(Sums * 100) / 100;
                    //总计
                    var AllSums = $("#list1").jqGrid('getGridParam', 'userData').sums;
                    $(this).footerData("set", { "StoreName": "<font color='red'>" + "页合计:" + "<font>", "Sum": "<font color='red'>" + Sums + "<font>" });
                    $newFooterRow.find("td[aria-describedby*='StoreName']").text("总合计:");
                    $newFooterRow.find("td[aria-describedby*='StoreName']").css("color", "red");
                    $newFooterRow.find("td[aria-describedby*='Sum']").text(AllSums);
                    $newFooterRow.find("td[aria-describedby*='Sum']").css("color", "red");

                } else {
                    $(".ui-jqgrid-sdiv").hide();
                }
            },
            userDataOnFooter: true,

        });
        $(window).bind('resize', function () {
            $("#list1").setGridWidth($(window).width() - 20);
            $("#list1").setGridHeight($(window).height() - 150);
        }).trigger('resize');

    });
   

    $(function () {
        $.hands = {
            Search: function () {     
                datas = $('#form1').formGetModel();
                $.loading(true, "正在查询数据");
                window.setTimeout(function () {
                    $("#list1").jqGrid('setGridParam', {
                        postData: { 'Search': datas},
                        //page: 1
                    }).trigger("reloadGrid");

                    $.loading(false);
                }, 500);
                var startdate = $("#StartDate").val();
                $("#StartTime").html(startdate);
                var enddate = $("#EndDate").val();

                if (enddate != undefined && enddate != "" && enddate != null) {

                    $("#EndTime").html(enddate);
                }
                else {
                    $("#EndTime").html('现在');
                }
              
                $('#list-Search').hide();
            },
            Excel: function () {
                //$.loading(true, "正在提交数据");
                //window.setTimeout(function () {                    
                //    var funFrom = $("#form1");
                //    funFrom.attr("method", "post")
                //    funFrom.attr("action", "/CGTJReport/GetFile/")
                //    var total = $("#list1").jqGrid('getGridParam', 'records');
                //    if (parseInt(total) > 0) {
                //        funFrom.submit();
                //    } else {
                //        top.layer.msg("空数据导出失败", { icon: 2 });
                //    }

                //}, 500);
                //$.loading(false);

                //后台通过string格式接受
                $.loading(true, "正在提交数据");
                window.setTimeout(function () {
                    var funFrom = document.createElement("form");
                    document.body.appendChild(funFrom);
                    funFrom.action = '/CGTJReport/GetFile/';
                    funFrom.method = 'post';
                    var total = $("#list1").jqGrid('getGridParam', 'records');
                    if (parseInt(total) > 0) {
                        var newStrSearch = document.createElement("input");
                        newStrSearch.setAttribute("name", "JsonSearch");
                        newStrSearch.setAttribute("type", "hidden");
                        newStrSearch.setAttribute("value", JSON.stringify(datas));
                        funFrom.appendChild(newStrSearch);
                        funFrom.submit();
                    } else {
                        top.layer.msg("空数据导出失败", { icon: 2 });
                    }

                }, 500);
                $.loading(false);
            },
            BtnSearch: function () {
                var display = $('#list-Search').css('display');
                if (display == 'none') {
                    $("#list-Search").show()

                } else {
                    $('#list-Search').hide();

                }

            },
            Clear: function () {
                $('#form1')[0].reset();  
                var strdate = new Date().Format("yyyy-MM-dd");
                $("#StartDate").val(strdate);

            },
            init: function () {
                $('#Search').on('click', $.hands.Search);
                $('#Clear').on('click', $.hands.Clear);
                $('#Excel').on('click', $.hands.Excel);
                $('#btn_Search').on('click', $.hands.BtnSearch);

            }
        }
        $(function () {
            $.hands.init();
        });
    });

</script>

<style>
    #form1 > td, th {
        padding: 5px 0px 5px 0px;
    }

    .ui-jqgrid {
        z-index: 99;
    }
</style>













